package TicketingSystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangePasswordFrame extends JFrame {
    public ChangePasswordFrame() {
        setTitle("修改密码");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new GridLayout(0, 1, 10, 10));
        setResizable(false);

        // 标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel titleLabel = new JLabel("修改密码");
        titleLabel.setFont(new Font("隶书", Font.BOLD, 30));
        titlePanel.add(titleLabel);

        // 用户名输入面板
        JPanel userPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel userLabel = new JLabel("用户名：");
        userLabel.setFont(new Font("楷书", Font.PLAIN, 24));
        JTextField userField = new JTextField(20);
        userPanel.add(userLabel);
        userPanel.add(userField);

        // 旧密码输入面板
        JPanel oldPassPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel oldPassLabel = new JLabel("旧密码：");
        oldPassLabel.setFont(new Font("楷书", Font.PLAIN, 24));
        JPasswordField oldPassField = new JPasswordField(20);
        oldPassPanel.add(oldPassLabel);
        oldPassPanel.add(oldPassField);

        // 新密码输入面板
        JPanel newPassPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel newPassLabel = new JLabel("新密码：");
        newPassLabel.setFont(new Font("楷书", Font.PLAIN, 24));
        JPasswordField newPassField = new JPasswordField(20);
        newPassPanel.add(newPassLabel);
        newPassPanel.add(newPassField);

        // 确认按钮面板
        JPanel confirmPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton confirmBtn = new JButton("确认");
        confirmBtn.setFont(new Font("楷书", Font.PLAIN, 24));
        confirmBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userField.getText();
                String oldPassword = new String(oldPassField.getPassword());
                String newPassword = new String(newPassField.getPassword());
                boolean isChanged = UserService.changePassword(username, oldPassword, newPassword);
                if (isChanged) {
                    JOptionPane.showMessageDialog(ChangePasswordFrame.this, "密码修改成功！");
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(ChangePasswordFrame.this, "密码修改失败，请检查输入信息。");
                }
            }
        });
        confirmPanel.add(confirmBtn);

        // 添加各个面板到窗口
        add(titlePanel);
        add(userPanel);
        add(oldPassPanel);
        add(newPassPanel);
        add(confirmPanel);

        // 设置窗口大小和位置
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
}